package otherapi.itext.kernellayoutpdf;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.CalRgb;
import com.itextpdf.kernel.colors.Color;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.borders.DottedBorder;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.properties.HorizontalAlignment;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import com.itextpdf.text.FontFactory;
import org.junit.Test;
import otherapi.itext.ItextPdfConst;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 1/5/2023 9:04 AM
 */
public class ItextKernelPdfTest {

    private static final String FILENAME = "test.pdf";

    @Test
    public void test4() throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Document document = getDocument(byteArrayOutputStream);
        OutputStream out = Files.newOutputStream(Paths.get(FILENAME));
        // 增加页眉
        document.add(getPdfHeader(document));
        // 增加段落
        document.add(getParagraph());
        // 增加列表
        document.add(getList());
        // 增加表格
        document.add(getTable());
        // 增加图片
        document.add(getPicture());
        // 增加一个新区域
        document.add(getAreaBreak());
        document.close();
        byteArrayOutputStream.writeTo(out);
        out.close();
        byteArrayOutputStream.close();
    }

    /**
     * 获取页眉
     *
     * @param document document
     * @return Cell
     * @throws IOException ioexception
     */
    private Cell getPdfHeader(Document document) throws IOException {
        Cell cell = new Cell();
        // 获取中文字体
        PdfFont chineseFont = PdfFontFactory.createFont("src/main/resources/fonts/STSong.ttf", PdfEncodings.IDENTITY_H);
        Paragraph chineseText = new Paragraph("我是页眉");
        chineseText.setFont(chineseFont);
        cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
        cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
        cell.setTextAlignment(TextAlignment.CENTER);
        cell.add(chineseText);
        cell.setHeight(30);
        // float documentAvailableHeight = ItextKernelUtils.getDocumentAvailableHeight(document);
        // float value = cell.getHeight().getValue();
        // ItextKernelUtils.setDocumentAvailableHeight(document, documentAvailableHeight - value);
        return cell;
    }

    /**
     * 获取列表
     *
     * @return List
     */
    private com.itextpdf.layout.element.List getList() {
        com.itextpdf.layout.element.List list = new com.itextpdf.layout.element.List();
        // Add elements to the list
        list.add("Java");
        list.add("JavaFX");
        list.add("Apache Tika");
        list.add("OpenCV");
        list.add("WebGL");
        list.add("Coffee Script");
        list.add("Java RMI");
        list.add("Apache Pig");
        return list;
    }

    /**
     * 增加新的一页
     *
     * @return
     */
    private AreaBreak getAreaBreak() {
        AreaBreak areaBreak = new AreaBreak();
        areaBreak.setPageSize(PageSize.A5);
        return areaBreak;
    }

    /**
     * 获取图片
     *
     * @return Cell
     * @throws MalformedURLException Exception
     */
    private Cell getPicture() throws MalformedURLException {
        Cell cell = new Cell();
        Image image = new Image(ImageDataFactory.create("src/main/resources/picture/one.jpg"));
        // 设置等比例缩放
        image.setAutoScale(true);
        cell.add(image);
        return cell;
    }

    /**
     * 获取表格
     *
     * @return IBlockElement
     * @throws IOException io Exception
     */
    private IBlockElement getTable() throws IOException {
        Table table = new Table(ItextKernelUtils.TABLE_PROPORTION);
        table.setMargins(5, 5, 5, 5);
        List<Cell> tableCell = getTableCell();
        for (Cell cell : tableCell) {
            table.addCell(cell);
        }
        return table;
    }

    /**
     * 获取表格中的单元格
     *
     * @return List
     * @throws IOException Io Exception
     */
    private List<Cell> getTableCell() throws IOException {
        List<Cell> cellList = new ArrayList<>();
        Cell cell;
        for (int i = 0; i < 20; i++) {
            cell = new Cell();
            cell.add(getCellParagraph(i + 1));
            cellList.add(cell);
        }
        return cellList;
    }

    private IBlockElement getCellParagraph(int i) throws IOException {
        // 获取中文字体
        PdfFont chineseFont = PdfFontFactory.createFont("src/main/resources/fonts/STSong.ttf", PdfEncodings.IDENTITY_H);
        Paragraph chineseText = new Paragraph("单元格内容" + i);
        chineseText.setFont(chineseFont);
        return chineseText;
    }

    /**
     * 获取段落
     *
     * @return Paragraph
     * @throws IOException Io exception
     */
    private Paragraph getParagraph() throws IOException {
        // 获取中文字体
        PdfFont chineseFont = PdfFontFactory.createFont("src/main/resources/fonts/STSong.ttf", PdfEncodings.IDENTITY_H);
        // 获取英文字体
        PdfFont englishFont = PdfFontFactory.createFont(FontFactory.TIMES_ROMAN);
        Paragraph englishText = new Paragraph("Hello world!");
        englishText.setFont(englishFont);
        Paragraph chineseText = new Paragraph("你好早安！");
        chineseText.add(englishText);
        chineseText.setFont(chineseFont);
        chineseText.setBorder(getSolidBorder(ItextPdfConst.BLACK));
        chineseText.setFontColor(ColorConstants.RED, 2);
        return chineseText;
    }

    /**
     * 获取 Document
     *
     * @param byteArrayOutputStream byte array output stream
     * @return Document
     */
    private Document getDocument(ByteArrayOutputStream byteArrayOutputStream) {
        PdfWriter pdfWriter = new PdfWriter(byteArrayOutputStream, new WriterProperties().setFullCompressionMode(true));
        PdfDocument pdfDoc = new PdfDocument(pdfWriter);
        // 创建文档对象 设置文档大小为 A4
        Document document = new Document(pdfDoc, PageSize.A4);
        document.setBorder(new DottedBorder(1));
        // 设置 document 的 margin
        document.setMargins(10, 10, 10, 10);
        document.setBackgroundColor(getColor(ItextPdfConst.BLACK));
        return document;
    }


    private Border getSolidBorder(String colorName) {
        return new SolidBorder(getColor(colorName), 1);
    }

    private Color getColor(String type) {
        float[] colorRgb;
        if (ItextPdfConst.BLACK.equals(type)) {
            colorRgb = new float[]{0, 0, 0};
        } else if (ItextPdfConst.RED.equals(type)) {
            colorRgb = new float[]{255, 0, 0};
        } else {
            colorRgb = new float[]{220, 220, 220};
        }
        return new CalRgb(colorRgb, colorRgb);
    }
}
